package com.learn.rpc.socket;

import com.learn.rpc.common.AdminUser;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * 客户端使用Socket的方式远程调用服务端提供的方法和信息
 * @author wangxing
 * @version 2020/8/14 17:16 Administrator
 */
public class RpcClient {
    public static void main(String[] args) throws IOException {
        String host ="localhost";
        int port=8899;
        Socket s = new Socket(host,port);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(123);

        s.getOutputStream().write(baos.toByteArray());
        s.getOutputStream().flush();

        DataInputStream dis = new DataInputStream(s.getInputStream());
        int id = dis.readInt();
        String name = dis.readUTF();
        AdminUser user = new AdminUser(id,name);
        System.out.println(user);
        dos.close();
        s.close();
        baos.close();
        dis.close();
    }
}
